package com.yunfei.api.upload;

import com.yunfei.api.upload.result.ResultModel;
import com.yunfei.mapper.crmdb.TblobtSorageMapper;
import com.yunfei.utils.IdUtils;
import com.yunfei.utils.contants.ApplicationConstant;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.support.StandardMultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * Created by Administrator on 2016/10/20.
 */
@Controller
public class UploadFileApiController {
    @Autowired
    private Environment env;
    @Autowired
    private TblobtSorageMapper tblobtSorageMapper;

    @PostMapping("/uploadAnswerFile")
    @Transactional
    @ApiOperation(value = "上传", notes = "", position = 1, response = Object.class)
    public Object uploadAnswerFile(HttpServletRequest request,String fileNo, String yewuType, String proj_id, String answerId, HttpServletResponse response) {
        //123
      //  String fileNamePre = jiaokuanService.getTblobStorageSeq()+"";
        String fileNamePre = "";
        ResultModel resultModel = new ResultModel();
        response.setHeader("Access-Control-Allow-Origin", "*");
        StandardMultipartHttpServletRequest standardMultipartHttpServletRequest = (StandardMultipartHttpServletRequest) request;
        InputStream inputStream = null;
        try {
            inputStream = standardMultipartHttpServletRequest.getMultiFileMap().get("file").get(0).getInputStream();
            FileInputStream fileInputStream = (FileInputStream) inputStream;
            MultipartFile multipartFile = new MockMultipartFile("temp.jpg", "asdf.jpg", "", inputStream);
            Long fileSize = ((MockMultipartFile) multipartFile).getSize();
            String uploadPath = env.getProperty(ApplicationConstant.FILE_IMAGE_PATH);
            String imageDomain = env.getProperty(ApplicationConstant.IMAGE_SERVER_DOMAIN) + "files/";
            String nextPath = "";
            if (yewuType != null && yewuType.equals("prodAnswer")) {
                nextPath = "PRODANSW" + "/" + proj_id + "/" + answerId;
            } else if (yewuType != null && yewuType.equals("jiaokuan")) {
                nextPath = "JIAOKUAN/"+fileNo;
            }else if (yewuType != null && yewuType.equals("hetong")) {
                nextPath = "HETONG/"+fileNo;
            }

            String imgPath = uploadPath + nextPath + "/";//本地存储路径
            System.out.println("文件上传路径前缀：" + uploadPath);
            System.out.println("文件查看路径前缀：" + imageDomain);
            String preFileName = "";
            if(yewuType!=null && yewuType.equals("prodAnswer")){
                preFileName = IdUtils.getId();
            }else if (yewuType != null && (yewuType.equals("jiaokuan")||yewuType.equals("hetong"))){
                preFileName = fileNamePre;
            }
            String filename = uploadFile(multipartFile, imgPath,preFileName);//上传文件名称
            String fileUrl = imageDomain + nextPath + "/" + filename;
            System.out.println("文件查看路径：" + fileUrl);
            System.out.println("文件上传路径：" + imgPath);
            System.out.println("文件" + filename);
            String imgName = multipartFile.getOriginalFilename();//上传文件的真实名称
            String suffixName = imgName.substring(imgName.lastIndexOf("."));

        } catch (IOException e) {
            e.printStackTrace();
        }
//        '',--传入的附件序号
//        '',--文件名称
//        '',--文件大小 字节
//        '',--创建人，员工编号
//        sysdate,--创建时间
//        '0',
//                '',--所属部门 select c_deptnodecode from tfundbroker where C_BROKERACCOUNT = 员工编号
//        sys_guid())
        return resultModel;
    }

    public String uploadFile(MultipartFile file, String path,String preFileName) {
        String imgName = file.getOriginalFilename();//上传文件的真实名称
        String suffixName = imgName.substring(imgName.lastIndexOf("."));//获取后缀名
        String acc_id = preFileName + suffixName;
        String fileName = acc_id;
        File fileUpload = null;
        try {
            File fileDic = new File(path);
            if (!fileDic.exists()) {
                fileDic.mkdirs();
            }
            fileUpload = new File(path + fileName);
            if (fileUpload.exists()) {
                fileUpload.delete();
            }
            fileUpload.createNewFile();

            //file.transferTo(fileUpload);
            FileUtils.copyInputStreamToFile(file.getInputStream(), fileUpload);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return fileUpload.getName();
    }
}
